﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class CustomerService
    {
        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<Customer> GetUserMes(int limit, int page, string CustName, string Sex, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Customer.Where(p => true);
                if (!string.IsNullOrEmpty(CustName))
                {
                    query = query.Where(p => p.CustName.Contains(CustName));
                }
                if (!string.IsNullOrEmpty(Sex))
                {
                    query = query.Where(p => p.Sex== Sex);
                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }
        /// <summary>
        /// 根据客户姓名查找客户信息
        /// </summary>
        /// <param name="CustName"></param>
        /// <returns></returns>
        public static Customer GetCustomer(string CustName)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Customer.FirstOrDefault(p=>p.CustName== CustName);
            }
        }
        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CustName"></param>
        /// <param name="Birthday"></param>
        /// <param name="Phone"></param>
        /// <returns></returns>
        public static int Update( int Id,string CustName, DateTime Birthday, string Phone,string Sex)
        {

            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer customer = db.Customer.FirstOrDefault(p => p.Id == Id);

                customer.CustName = CustName;
                customer.Birthday = Birthday;
                customer.Phone = Phone;
                customer.Sex = Sex;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="CustName"></param>
        /// <param name="Pwd"></param>
        /// <param name="Phone"></param>
        /// <returns></returns>
        public static int AddCustomer(string CustName, string Pwd, string Phone,string Sex)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer customer = new Customer
                {
                    CustName = CustName,
                    Pwd = Pwd,
                    Phone=Phone,
                    Sex=Sex
                };
                db.Customer.Add(customer);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static int GetBatchDelByIds(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"delete [Customer] where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDel(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer customer = new Customer() { Id = id };
                db.Customer.Attach(customer);
                db.Customer.Remove(customer);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Customer GetSelec(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Customer.FirstOrDefault(p => p.Id == id);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="RealName"></param>
        /// <param name="Age"></param>
        /// <param name="Mobile"></param>
        /// <param name="realId"></param>
        /// <returns></returns>
        public static int GetUpdate(string CustName, string Sex, string Birthday, string Phone, int realId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer customer = db.Customer.SingleOrDefault(p => p.Id == realId);
                customer.CustName = CustName;
                customer.Sex  = Sex;
                customer.Birthday = Convert.ToDateTime(Birthday);
                customer.Phone = Phone;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="NewPassWord"></param>
        /// <returns></returns>
        public static int GetUpdatePwd(string CustName, string NewPwd)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer userMes = db.Customer.SingleOrDefault(p => p.CustName == CustName);
                userMes.Pwd = NewPwd;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetRestPwd(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Customer userMes = db.Customer.SingleOrDefault(p => p.Id == id);
                userMes.Pwd = "e10adc3949ba59abbe56e057f20f883e";
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据用户名查找用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static Customer GetUser(string CustName)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Customer.FirstOrDefault(p => p.CustName == CustName);
            }
        }
    }
}
